package ch5;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
//验证登录
@WebServlet(name = "ToLoginServlet",value = "/toLogin")
public class ToLoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = null;
        if ("zhangsan".equals(username) && "123456".equals(password)) {
            //验证账户密码，正确登录
            user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setVIP(false);
//            HttpSession session = request.getSession();
//            session.setAttribute("user", new User(username, password));
//            Cookie cookie = new Cookie("JSESSIONID",session.getId());
//            cookie.setMaxAge(5*60);
//            response.addCookie(cookie);
//            response.sendRedirect("index");
            // 设置选项七天免登录
            String rememberMe = request.getParameter("rememberMe");
            if ("true".equals(rememberMe)) {
                // 设置cookie1
                Cookie cookie1 = new Cookie("autoLogin", username);
                // 设置cookie1过期时间为7天
                cookie1.setMaxAge(7 * 24 * 60 * 60); // 7天 = 7 * 24小时/天 * 60分钟/小时 * 60秒/分钟
                response.addCookie(cookie1);
            }
            response.sendRedirect("ch5_jstl.jsp");
        }else if("gaoshuaikai".equals(username) && "114514".equals(password)){
            //验证账户密码，正确登录
            user = new User();
            user.setUsername(username);
            user.setPassword(password);
//            HttpSession session = request.getSession();
//            session.setAttribute("user", new User(username, password));
            user.setVIP(true);
//            Cookie cookie = new Cookie("JSESSIONID",session.getId());
//            cookie.setMaxAge(5*60);
//            response.addCookie(cookie);
//            response.sendRedirect("index");
            // 设置选项七天免登录
            String rememberMe = request.getParameter("rememberMe");
            if ("true".equals(rememberMe)) {
                // 设置cookie1
                Cookie cookie1 = new Cookie("autoLogin", username);
                // 设置cookie1过期时间为7天
                cookie1.setMaxAge(7 * 24 * 60 * 60); // 7天 = 7 * 24小时/天 * 60分钟/小时 * 60秒/分钟
                response.addCookie(cookie1);
            }

//            // 重定向到首页
            response.sendRedirect("ch5_jstl.jsp");
        }
        if (user != null) {
            // 验证成功，将用户信息保存到HttpSession中
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            return;
//            // 重定向到权限页面
//            response.sendRedirect("ch5_jstl.jsp");
        } else {
            // 登录失败处理
            request.setAttribute("error", "你的用户名或密码错误");
            request.getRequestDispatcher("login.html").forward(request, response);
        }
    }
}
//        String username = request.getParameter("username");
//        String password = request.getParameter("password");
//        User user = null;
//
//        if ("zhangsan".equals(username) && "123456".equals(password)) {
//        user = new User();
//        user.setUsername(username);
//        user.setPassword(password);
//        user.setVIP(false);
//        } else if ("gaoshuaikai".equals(username) && "114514".equals(password)) {
//        user = new User();
//        user.setUsername(username);
//        user.setPassword(password);
//        user.setVIP(false); // 假设这个用户不是VIP，除非你知道他是
//        }
//
//        if (user != null) {
//        // 将user对象存储到请求作用域中
//        request.setAttribute("user", user);
//        response.sendRedirect("ch5_jstl.jsp");
//        return; // 结束方法执行
//        } else {
//            // 登录失败处理
//            request.setAttribute("error", "你的用户名或密码错误");
//            request.getRequestDispatcher("login.html").forward(request, response);
//        }
//    }
//}